﻿using Lz.HttpDemo.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lz.HttpDemo
{
    /// <summary>
    /// 接口服务
    /// </summary>
    public class LzHttpService
    {
        /// <summary>
        /// 基础地址
        /// </summary>
        public string Host = "http://localhost:50002/";

        /// <summary>
        ///  POST请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parms">参数</param>
        /// <returns></returns>
        public string Post(string url, object parms = null)
        {
            url = Host + url;
            var p = new HttpClientContent()
            {
                Url = url
            };

            if (parms != null)
            {
                p.Data = parms.ToJson();
            }
            return HttpClientService.PostString(p);
        }

        /// <summary>
        /// POST请求返回接口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public HttpMessageResult<T> PostResult<T>(string url, object parms = null)
        {
            return Post(url, parms).JsonToObject<HttpMessageResult<T>>();
        }

        /// <summary>
        /// 直接返回数据结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public T PostDataResult<T>(string url, object parms = null)
        {
            var rs = Post(url, parms).JsonToObject<HttpMessageResult<T>>();

            if (rs.Code == 0) 
            {
                return rs.Data;
            }
            return default;
        }

    }
}
